﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PointTracker
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!IsPostBack)
            {
                if (Session["User"] == null)
                {
                    Response.Redirect("/");
                }
                else if (((User)Session["User"]).role != UserRole.Professor)
                {
                    Response.Redirect("/");
                }
            }
            TextBoxScribeFname.Enabled = CheckBoxScribe.Checked;
            TextBoxScribeLname.Enabled = CheckBoxScribe.Checked;
            TextBoxScribeEmail.Enabled = CheckBoxScribe.Checked;
            TextBoxScribePassword.Enabled = CheckBoxScribe.Checked;
        }

        public void AddClass(Object sender, EventArgs e)
        {
            if (!TextBoxClassName.Text.Trim().Equals("") &&
                !TextBoxCRN.Text.Trim().Equals("") &&
                !TextBoxTerm.Text.Trim().Equals("") &&
                !TextBoxYear.Text.Equals("") &&
                CheckBoxListDays.SelectedIndex != -1)
            {
                days selectedDays = new days();
                if (CheckBoxListDays.Items[0].Selected) // Sunday
                    selectedDays += 1;
                if (CheckBoxListDays.Items[1].Selected) // Monday
                    selectedDays += 2;
                if (CheckBoxListDays.Items[2].Selected) // Tuesday
                    selectedDays += 4;
                if (CheckBoxListDays.Items[3].Selected) // Wednesday
                    selectedDays += 8;
                if (CheckBoxListDays.Items[4].Selected) // Thursday
                    selectedDays += 16;
                if (CheckBoxListDays.Items[5].Selected) // Friday
                    selectedDays += 32;
                if (CheckBoxListDays.Items[6].Selected) // Saturday
                    selectedDays += 64;
                //Response.Write((int)selectedDays);

                Boolean successful = PointTracker.DatabaseManager.addClass(Int32.Parse(TextBoxCRN.Text), TextBoxClassName.Text, (int)selectedDays, Int32.Parse(TextBoxTerm.Text), Int32.Parse(TextBoxYear.Text), ((User)Session["User"]).email);

                Boolean successfulScribe = false;
                if (CheckBoxScribe.Checked)
                    successfulScribe = PointTracker.User.adduser(TextBoxScribeFname.Text, TextBoxScribeLname.Text, TextBoxScribeEmail.Text, TextBoxScribePassword.Text, PointTracker.UserRole.Scribe, Int32.Parse(TextBoxCRN.Text));
                else
                    successfulScribe = true;

                if (successful && successfulScribe)
                {
                    Response.Redirect("~/Professor.aspx");
                }
                else
                {
                    Response.Redirect("~/Error/Default.aspx");
                }
            }
        }
    }
}
